package com.pxmeta.party.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.party.entity.Gift;
import com.pxmeta.party.entity.UserGiftWall;
import com.pxmeta.party.entity.vo.UserGiftWallListVO;
import com.pxmeta.party.entity.vo.UserGiftWallVO;
import com.pxmeta.party.mapper.UserGiftWallMapper;
import com.pxmeta.party.service.IGiftService;
import com.pxmeta.party.service.IUserGiftWallService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserGiftWallServiceImpl extends ServiceImpl<UserGiftWallMapper, UserGiftWall> implements IUserGiftWallService {

    @Autowired
    private IGiftService giftService;

    @Override
    public void addUserGiftWall(Long targetUserId, Integer giftId, Integer giftCount) {
        LambdaQueryWrapper<UserGiftWall> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserGiftWall::getUserId, targetUserId)
                .eq(UserGiftWall::getGiftId,giftId);

        UserGiftWall userGiftWall = this.getOne(wrapper);
        if (null == userGiftWall){
            UserGiftWall newGiftWall = new UserGiftWall();
            newGiftWall.setGiftId(giftId);
            newGiftWall.setUserId(targetUserId);
            newGiftWall.setCount(giftCount);
            this.save(newGiftWall);
        }else {
            Integer count = userGiftWall.getCount();
            count += giftCount;
            userGiftWall.setCount(count);
            this.updateById(userGiftWall);
        }
    }

    @Override
    public UserGiftWallListVO getUserGiftWallList(Long userId) {
        List<Gift> allGiftList = giftService.getAllGiftList();
        LambdaQueryWrapper<UserGiftWall> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserGiftWall::getUserId, userId);
        List<UserGiftWall> list = this.list(wrapper);

        List<UserGiftWallVO> finalList = Lists.newLinkedList();
        for (Gift gift : allGiftList) {
            UserGiftWallVO vo = new UserGiftWallVO();
            Optional<UserGiftWall> opt = list.stream().filter(w -> w.getGiftId().equals(gift.getId())).findFirst();
            vo.setIcon(gift.getIcon());
            vo.setIsOpen(Boolean.FALSE);
            vo.setPrice(gift.getPrice());
            vo.setName(gift.getName());
            opt.ifPresent(userGiftWall -> {
                vo.setIsOpen(Boolean.TRUE);
                vo.setCount(userGiftWall.getCount());
            });
            finalList.add(vo);
        }

        UserGiftWallListVO vo = new UserGiftWallListVO();
        vo.setList(finalList);
        vo.setCurrentCount(list.size());
        vo.setTotalCount(allGiftList.size());

        return vo;
    }

    @Override
    public UserGiftWallListVO getUserGiftWallIntro(Long userId) {


        List<Gift> allGiftList = giftService.getAllGiftList();
        List<Integer> giftIds = allGiftList.stream().filter(a -> a.getPrice() > 5000).map(Gift::getId).collect(Collectors.toList());

        LambdaQueryWrapper<UserGiftWall> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserGiftWall::getUserId, userId)
                .in(UserGiftWall::getGiftId, giftIds)
                .last("limit 4");
        List<UserGiftWall> list = this.list(wrapper);
        List<UserGiftWallVO> finalList = Lists.newLinkedList();
        for (UserGiftWall userGiftWall : list) {
            Optional<Gift> opt = allGiftList.stream().filter(w -> w.getId().equals(userGiftWall.getGiftId())).findFirst();
            UserGiftWallVO vo = new UserGiftWallVO();
            if (opt.isPresent()){
                Gift gift = opt.get();
                vo.setIcon(gift.getIcon());
                vo.setPrice(gift.getPrice());
                vo.setCount(userGiftWall.getCount());
                vo.setIsOpen(Boolean.TRUE);
                finalList.add(vo);
            }
        }
        wrapper.clear();
        wrapper.eq(UserGiftWall::getUserId, userId);
        long currentCount = this.count(wrapper);
        UserGiftWallListVO vo = new UserGiftWallListVO();
        vo.setTotalCount(allGiftList.size());
        vo.setCurrentCount((int) currentCount);
        vo.setList(finalList);

        return vo;
    }
}
